package com.fancy.job.config;

import com.fancy.config.RedisConfig;
import com.fancy.job.Quartz;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    //1.创建job对象
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //关联job类
        factoryBean.setJobClass(Quartz.class);
        return factoryBean;
    }

    //2.创建Trigger对象
    /*@Bean
    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        //关联jobDetail对象
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        //参数表示执行的毫秒数
        factoryBean.setRepeatInterval(2000);
        //设置重复次数
        factoryBean.setRepeatCount(5);

        return factoryBean;
    }*/
    //Cron trigger
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        //创建CronTriggerFactoryBean对象
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        //设置关联JobDetailFactoryBean对象
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        //设置出发时间
        factoryBean.setCronExpression("0/30 * * * * ?");

        return  factoryBean;
    }


    //3.创建Scheduler对象
    /*@Bean
    public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean) {
        //创建SchedulerFactoryBean对象
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        //关联SchedulerFactoryBean
        factoryBean.setTriggers(simpleTriggerFactoryBean.getObject());

        return factoryBean;
    }*/
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) {
        //创建SchedulerFactoryBean对象
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        //关联SchedulerFactoryBean
        factoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        return factoryBean;
    }
}
